//
//  LSZLyricBooksVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2017/9/29.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LSZLyricBooksVC.h"
#import "LSZLyricBooksVC+LCAVPlay.h"

@interface LSZLyricBooksVC ()

@end

@implementation LSZLyricBooksVC

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [LCFloatingWindow sharedInstance].hidden = NO;////
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [LCFloatingWindow sharedInstance].hidden = YES;///
}
- (void)viewDidLoad {
    [super viewDidLoad];
    /////
    [self creareAVPlay];
    LCPlaylist *playlistTest = _PlaylistMgr.allPlaylists.firstObject;
    _LCPlayer.songs = playlistTest.allSongs;
    
    if (_LCPlayer.songs != nil) {// && _LCPlayer.currentTime == 0
        [_LCPlayer playAtIndex:_lrcIndex isSelectPlay:YES];
    }

    self.title = [NSString stringWithFormat:@"%@",self.titleStr];
   
    self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-60)];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.URL]]];
    [self.view addSubview:self.webView];
    
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 60, 40);
    [rightBtn setTitle:[NSString stringWithFormat:@"%zd", self.lrcIndex+1] forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(changeLrcRightBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    [self setUI];
}
#pragma mark - 创建选择歌曲的弹窗
-(void)setUI
{
    self.selectNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 80, 40)];
    _selectNumLabel.textAlignment = NSTextAlignmentCenter;
    _selectNumLabel.font = FONT_18;
    _selectNumLabel.text = @"";
    CGFloat width = 240;
    CGFloat height = 160;
    //筛选歌曲
    self.selectView = [[UIView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, self.view.width, self.view.height-NAVIGATION_BAR_HEIGHT-60-HOME_INDICATOR_HEIGHT)];
    _selectView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    _selectView.alpha = 0;
    [self.view addSubview:_selectView];
    
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(width/3, height/4);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.headerReferenceSize = CGSizeMake(width, 40);
    
    self.selectCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake((self.view.width-width)/2, 120, width, height+40) collectionViewLayout:flowLayout];
    _selectCollectionView.delegate = self;
    _selectCollectionView.dataSource = self;
    _selectCollectionView.backgroundColor = [UIColor whiteColor];
    [_selectCollectionView registerClass:[ShengJNumCollectionViewCell class] forCellWithReuseIdentifier:@"collectionViewCell"];
    [_selectCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:@"UICollectionElementKindSectionHeader" withReuseIdentifier:@"headerID"];
    [_selectView addSubview:_selectCollectionView];
}
#pragma mark - 打开歌曲选择
-(void)changeLrcRightBtnAction
{
    _selectNumLabel.text = @"";
    [self.view bringSubviewToFront:_selectView];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.4];
    _selectView.alpha = 1;
    [UIView commitAnimations];
}
#pragma mark - 关闭歌词选择
- (void)offLrcSelect
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.4];
    _selectView.alpha = 0;
    [UIView commitAnimations];
}
#pragma mark - 集合视图代理
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 12;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *tempView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerID" forIndexPath:indexPath];
    tempView.userInteractionEnabled = YES;
    [tempView removeAllSubviews];
    
    UILabel * tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 80, tempView.height)];
    tempLabel.font = FONT_14;
    tempLabel.text = @"请选择曲目:";
    [tempView addSubview:tempLabel];
    [tempView addSubview:_selectNumLabel];
    
    UIButton * offBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    offBtn.frame = CGRectMake(200, 0, 40, tempView.height);
    offBtn.userInteractionEnabled = YES;
    [offBtn setTitle:@"关闭" forState:UIControlStateNormal];
    [offBtn setTitleColor:JK_RED_COLOR forState:UIControlStateNormal];
    offBtn.titleLabel.font = FONT_16;
    [offBtn addTarget:self action:@selector(offLrcSelect) forControlEvents:UIControlEventTouchUpInside];
    [tempView addSubview:offBtn];
    //分割线
    UILabel * lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, tempView.height-0.5, tempView.width, 0.5)];
    lineLabel.backgroundColor = JK_HUISE_LINE_COLOR;
    [tempView addSubview:lineLabel];
    return tempView;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ShengJNumCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionViewCell" forIndexPath:indexPath];
    if (indexPath.row < 9) {
        cell.messageStr = [NSString stringWithFormat:@"%zd", indexPath.row+1];
        cell.titleLabel.font = FONT_22;
    }
    if (indexPath.row == 9) {
        cell.messageStr = [NSString stringWithFormat:@"0"];
        cell.titleLabel.font = FONT_22;
    }
    if (indexPath.row == 10) {
        cell.messageStr = @"删除";
        cell.titleLabel.font = FONT_18;
    }
    if (indexPath.row == 11) {
        cell.messageStr = @"跳转";
        cell.titleLabel.font = FONT_18;
    }
    //点击选择按钮的背景颜色
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = JK_RED_COLOR;
    cell.selectedBackgroundView = view;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    [self performSelector:@selector(hiddenMB) withObject:nil afterDelay:1.2];
    //点击数字显示
    if (indexPath.row < 9) {
        NSString * tempNumStr = [NSString stringWithFormat:@"%@%zd", _selectNumLabel.text, indexPath.row+1];
//        NSInteger tempNum = [tempNumStr integerValue];
        _selectNumLabel.text = tempNumStr;
        if (_selectNumLabel.text.length == 4) {
            NSString * tempNumStr = [_selectNumLabel.text substringToIndex:_selectNumLabel.text.length-1];
            _selectNumLabel.text = tempNumStr;
        }
    }
    if (indexPath.row == 9) {
        NSString * tempNumStr = [NSString stringWithFormat:@"%@%d", _selectNumLabel.text, 0];
//        NSInteger tempNum = [tempNumStr integerValue];
        _selectNumLabel.text = tempNumStr;
        if (_selectNumLabel.text.length == 4) {
            NSString * tempNumStr = [_selectNumLabel.text substringToIndex:_selectNumLabel.text.length-1];
            _selectNumLabel.text = tempNumStr;
        }
    }
#pragma mark - 回撤
    if (indexPath.row == 10) {
        if (_selectNumLabel.text.length == 0) {
            return;
        }
        NSString * tempNumStr = [_selectNumLabel.text substringToIndex:_selectNumLabel.text.length-1];
        _selectNumLabel.text = tempNumStr;
    }
#pragma mark - 跳转
    if (indexPath.row == 11) {
        NSInteger tempNum = [_selectNumLabel.text integerValue];
        if (_selectNumLabel.text.length == 0 || tempNum == 0) {
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
            hud.mode=MBProgressHUDModeText;//枚举类型不同的效果
            hud.label.text = @"序号不能为空";
            return;
        }
        //跳转刷新歌曲
        [self changeSomeMessAndPlaySong:tempNum];
        
    }
}
//播放选定歌曲  以及更换信息
- (void)changeSomeMessAndPlaySong:(NSInteger)no
{
    if (self.mutArrayData.count < no ) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode=MBProgressHUDModeText;//枚举类型不同的效果
        hud.label.text = @"没有该曲目";
        
        [self performSelector:@selector(hiddenMB) withObject:nil afterDelay:3.0];
    }else{
        _lrcIndex = no-1;
            [_LCPlayer playAtIndex:_lrcIndex isSelectPlay:YES];//        //play歌曲

        //当删除列表里的歌曲时做特殊处理跳转
        if (_LCPlayer.songs.count != _PlaylistMgr.allPlaylists[0].allSongs.count) {
            for (NSInteger i = 0; i < _PlaylistMgr.allPlaylists[0].allSongs.count; i++) {
                if ([_LCPlayer.playingSong.title isEqualToString:_PlaylistMgr.allPlaylists[0].allSongs[i].title]) {
                    //右边导航的选择歌曲按钮的显示
                    NSString * noStr = [NSString stringWithFormat:@"%zd", i+1];
                    UIButton * rightBtn = (UIButton *)self.navigationItem.rightBarButtonItem.customView;
                    [rightBtn setTitle:[NSString stringWithFormat:@"%@", noStr] forState:UIControlStateNormal];
                    LSZPoetryMg * poetry = nil;
                    poetry = self.mutArrayData[i];
                    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:poetry.image]]];
                    self.title = [NSString stringWithFormat:@"%@",poetry.title];
                    [self offLrcSelect];
                    return;
                }
            }
        }
        
        
        //右边导航的选择歌曲按钮的显示
        NSString * noStr = [NSString stringWithFormat:@"%zd", no];
        UIButton * rightBtn = (UIButton *)self.navigationItem.rightBarButtonItem.customView;
        [rightBtn setTitle:[NSString stringWithFormat:@"%@", noStr] forState:UIControlStateNormal];
        LSZPoetryMg * poetry = nil;
        poetry = self.mutArrayData[_lrcIndex];
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:poetry.image]]];
        self.title = [NSString stringWithFormat:@"%@",poetry.title];
        [self offLrcSelect];
    }
}
- (void)hiddenMB
{
        [MBProgressHUD hideHUDForView:self.view animated:YES];
}
@end
